﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary
{
    public class Utils
    {
        public static void CreateAndWriteFile(string file, string text)
        {

            System.IO.StreamWriter streamWriter = null;
            try
            {
                string xmlString = text;
                var xmlFile = new System.IO.FileInfo(file);
                streamWriter = xmlFile.CreateText();
                streamWriter.WriteLine(xmlString);
                streamWriter.Close();
            }
            finally
            {
                if ((streamWriter != null))
                {
                    streamWriter.Dispose();
                }
            }

        }

        public static void PrintMap(char [][] map)
        {
            var _m = map.Count();
            var _n = map[0].Count();
            for (int y = _m - 1; y >= 0; y--)
            {
                for (int x = 0; x < _n; x++)
                {
                    Console.Write(map[y][x]);
                }
                Console.Write(Environment.NewLine);
            }
        }

        public static void CopyMap(char[][] map1, out char[][] map2)
        {
            var _m = map1.Count();
            var _n = map1[0].Count();
            map2 = new char[_m][];
            for (int y = 0; y < _m; y++)
            {
                map2[y] = new char[_n];
                for (int x = 0; x < _n; x++)
                {
                    map2[y][x] = map1[y][x];
                }
            }
        }


        public static void MapUpdate(ref char[][] map) // il faut définir des bornes de ce que l'on veut mettre à jour
        {
            var _m = map.Count();
            var _n = map[0].Count();
            char[][] mapTemp;
            CopyMap(map, out mapTemp);
            bool noLampdasRemaining = true;
            for (int y = 0; y < _m; y++)
            {
                for (int x = 0; x < _n; x++)
                {
                    if (mapTemp[y][x] == '\\')
                    {
                        noLampdasRemaining = false;
                    }
                }
            }

            for (int x = 0; x < _n; x++)
            {
                if (mapTemp[0][x] == 'L' && noLampdasRemaining)
                {
                    map[0][x] = 'O';
                }
            }


            for (int y = 1; y < _m; y++)
            {
                for (int x = 0; x < _n - 1; x++)
                {
                    if (mapTemp[y][x] == '*' && mapTemp[y - 1][x] == ' ')
                    {
                        map[y][x] = ' ';
                        map[y - 1][x] = '*';
                    }
                    if (mapTemp[y][x] == '*' && mapTemp[y - 1][x] == '*' && mapTemp[y][x + 1] == ' ' &&
                        mapTemp[y - 1][x + 1] == ' ')
                    {
                        map[y][x] = ' ';
                        map[y - 1][x + 1] = '*';
                    }
                    if (mapTemp[y][x] == '*' && mapTemp[y - 1][x] == '*' &&
                        (mapTemp[y][x + 1] != ' ' || mapTemp[y - 1][x + 1] != ' ') && mapTemp[y][x - 1] == ' ' &&
                        mapTemp[y - 1][x - 1] == ' ')
                    {
                        map[y][x] = ' ';
                        map[y - 1][x - 1] = '*';
                    }
                    if (mapTemp[y][x] == '*' && mapTemp[y - 1][x] == '\\' && mapTemp[y][x + 1] == ' ' &&
                        mapTemp[y - 1][x + 1] == ' ')
                    {
                        map[y][x] = ' ';
                        map[y - 1][x + 1] = '*';
                    }

                    if (mapTemp[y][x] == 'L' && noLampdasRemaining)
                    {
                        map[y][x] = 'O';
                    }
                }
            }
            for (int y = 1; y < _m; y++)
            {
                var x = _n - 1;
                if (mapTemp[y][x] == '*' && mapTemp[y - 1][x] == ' ')
                {
                    map[y][x] = ' ';
                    map[y - 1][x] = '*';
                }
            }
        }

    }
}
